package com.v9.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.v9.common.annotation.QueryField;
import com.v9.common.exception.BizException;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryDtoUtil {
    public static QueryWrapper buildQuery(Object o) {
        QueryWrapper query = new QueryWrapper();
        Field[] declaredFields = o.getClass().getDeclaredFields();
        Map<Field, List<Object>> groupList = new HashMap<>();

        Map<String, QueryField> queryTypes = new HashMap<>();
        for (Field item : declaredFields) {
            try {
                Object property = PropertyUtils.getProperty(o, item.getName());
                if (property == null) {
                    continue;
                }
                if (property instanceof String) {
                    if (StringUtils.isBlank((String) property)) {
                        continue;
                    }
                }
                QueryField queryField = item.getAnnotation(QueryField.class);
                String name = item.getName();

                List<Object> objects = groupList.get(name);
                if (objects == null) {
                    objects = new ArrayList<>();
                }
                objects.add(property);
                groupList.put(item, objects);
                queryTypes.put(name, queryField);
            } catch (Exception e) {
                throw new BizException("buildQuery.error");
            }
        }
        groupList.forEach((key, value) -> {
            QueryField queryField = queryTypes.get(key.getName());
            String name = key.getName();
            if (queryField != null && StringUtils.isNotBlank(queryField.property())) {
                name = queryField.property();
            }
            name =  BeanCoper.getColumnToField(name);;
            if (value.size() == 2) {
                query.between(name, value.get(0), value.get(1));
            } else {
                boolean dudata=false;
                if(queryField==null){
                    query.eq(name, value.get(0));
                    return;
                }
                if (queryField.queryType() == QueryType.LTE) {
                    query.le(name, value.get(0));
                    dudata=true;
                }
                if (queryField.queryType() == QueryType.LT) {
                    query.lt(name, value.get(0));
                    dudata=true;
                }
                if (queryField.queryType() == QueryType.LIKE) {
                    query.like(name, value.get(0));
                    dudata=true;
                }
                if (queryField.queryType() == QueryType.GTE) {
                    query.ge(name, value.get(0));
                    dudata=true;
                }
                if (queryField.queryType() == QueryType.GT) {
                    query.gt(name, value.get(0));
                    dudata=true;
                }
                if(dudata==false){
                    query.eq(name, value.get(0));
                }
            }
        });
        return query;
    }
}




